package com.myBase.core.facade.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.myBase.core.base.consts.ErrorMsgEnum;
import com.myBase.core.base.consts.ResultData;
import com.myBase.core.common.util.EBeiConstantEnum;
import com.myBase.core.entity.GoldCoinSettlementReq;
import com.myBase.core.entity.GoldCoinSettlementResp;
import com.myBase.core.entity.Goldcoin;
import com.myBase.core.entity.GoldcoinAccount;
import com.myBase.core.entity.GoldcoinAccountDetail;
import com.myBase.core.entity.GoldcoinOrder;
import com.myBase.core.entity.GoldcoinTradeRecord;
import com.myBase.core.entity.GrantGoldCoinsVo;
import com.myBase.core.entity.UseGoldCoinVo;
import com.myBase.core.facade.service.EBaiGoldCoinInPutService;
import com.myBase.core.facade.service.EBeiGoldCoinAccountServiceFacade;
import com.myBase.core.facade.service.GoldcoinAccountDetailServiceFacade;
import com.myBase.core.facade.service.GoldcoinAccountServiceFacade;
import com.myBase.core.facade.service.GoldcoinOrderServiceFacade;
import com.myBase.core.facade.service.GoldcoinTradeRecordServiceFacade;
import com.myBase.core.util.EBeiGoldCoinDateUtil;

/**
 *     
 * 项目名称：eBei-api    
 * 类名称：EBaiGoldCoinAccountServiceFacade    
 * 类描述：完成 ebei 金币账户相关的业务功能    
 * 创建人：wj    
 * 创建时间：2017年5月4日 下午2:06:09    
 * 修改人：wj    
 * 修改时间：2017年5月4日 下午2:06:09    
 * 修改备注：    
 * @version     
 *
 */
@Service("eBeiGoldCoinAccountServiceFacade")
public class EBeiGoldCoinAccountServiceFacadeImpl implements EBeiGoldCoinAccountServiceFacade {
    private final static Logger logger = Logger.getLogger(EBeiGoldCoinAccountServiceFacadeImpl.class);

    @Autowired
    private GoldcoinTradeRecordServiceFacade goldcoinTradeRecordService;

    @Autowired
    private GoldcoinAccountServiceFacade goldcoinAccountService;

    @Autowired
    private EBaiGoldCoinInPutService goldcoinService;

    @Autowired
    private GoldcoinAccountDetailServiceFacade goldcoinAccountDetailService;

    @Autowired
    private GoldcoinOrderServiceFacade goldcoinOrderService;

    @Override
    public ResultData<HashMap<String, Object>> GrantGoldCoins(GrantGoldCoinsVo grantGoldCoinsVo) {
        logger.info("访问开始，入参：" + grantGoldCoinsVo.toString());

        //订单表增加一条记录
        GoldcoinOrder goldcoinOrder = new GoldcoinOrder();
        goldcoinOrder.setOrderid(grantGoldCoinsVo.getOrderId());
        goldcoinOrder.setBranchid(grantGoldCoinsVo.getBranchId());
        goldcoinOrder.setSubchannel(grantGoldCoinsVo.getSubChannel());
        goldcoinOrder.setOrderStatus(EBeiConstantEnum.GOLDCOIN_ORDER_STATUS_INITIALIZE.toString());
        goldcoinOrder.setTradetime(grantGoldCoinsVo.getTradeTime());//交易时间
        goldcoinOrder.setTradetype(EBeiConstantEnum.GOLDCOIN_ORDER_TYPE_GAIN.toString());//发放金币
        goldcoinOrder.setGoldcoinsum(grantGoldCoinsVo.getGoldCoinSum());
        goldcoinOrder.setUserid(grantGoldCoinsVo.getUserId());
        goldcoinOrder.setGoldCoinId(grantGoldCoinsVo.getGoldCoidId());//goldcoin表id

        //流水表增加一条记录
        GoldcoinTradeRecord goldCoinTradeRecord = new GoldcoinTradeRecord();
        goldCoinTradeRecord.setAccountid(EBeiConstantEnum.GOLDCOIN_DEFAULT_VALUE.toString());//关联 goldcoin_account
        goldCoinTradeRecord.setDetailid(EBeiConstantEnum.GOLDCOIN_DEFAULT_VALUE.toString());//关联 goldcoin_account_detail
        goldCoinTradeRecord.setGoldcoinpoolid(grantGoldCoinsVo.getGoldCoidId());// 关联 金币表 goldCoin
        goldCoinTradeRecord.setRecordType(EBeiConstantEnum.GOLDCOIN_TRADERECORD_STATUS_GAIN.toString());//记录状态,00-获取，01-使用，02-过期，03冲正，04-作废
        goldCoinTradeRecord.setGoldnumber(grantGoldCoinsVo.getGoldCoinSum());//交易金币数量
        goldCoinTradeRecord.setBranchid(grantGoldCoinsVo.getBranchId());
        goldCoinTradeRecord.setRemarks(grantGoldCoinsVo.getRemarks());//描述信息，比如 注册获得10个金币
        goldCoinTradeRecord.setBusinessId(grantGoldCoinsVo.getOrderId());//交易流水号
        goldCoinTradeRecord.setNodetype(grantGoldCoinsVo.getNoteType());
        goldCoinTradeRecord.setUserid(grantGoldCoinsVo.getUserId() + "");
        goldCoinTradeRecord.setUserName(grantGoldCoinsVo.getUserName());
        goldCoinTradeRecord.setMobile(grantGoldCoinsVo.getMobile());
        goldCoinTradeRecord.setEquipment(grantGoldCoinsVo.getEquipment());

        //查询该用户是否存在
        GoldcoinAccount goldCoinAccount = goldcoinAccountService.getGoldcoinAccountCheck(grantGoldCoinsVo.getUserId(),
                grantGoldCoinsVo.getBranchId());

        //如果用户不为null,但是用户账户不是正常状态，则中止
        if (goldCoinAccount != null && (!EBeiConstantEnum.GOLDCOIN_ACCOUNT_STATUS_NORMAL.toString()
                .equals(goldCoinAccount.getAccountstatus()))) {
            logger.info("金币账户存在，但是用户账户处于非正常状态，中止");

            //记录错误信息
            goldcoinOrder.setRemarks("金币账户存在，但是用户账户处于非正常状态，中止");
            //订单状态-失败
            goldcoinOrder.setOrderStatus(EBeiConstantEnum.COLDCOIN_ORDER_STATUS_FALSE.toString());//发放失败
            //订单-保存
            goldcoinOrderService.save(goldcoinOrder);

            //组织返回参数，访问失败
            ResultData<HashMap<String, Object>> resultData = new ResultData<HashMap<String, Object>>(
                    ErrorMsgEnum.ERROR.getCode(), "金币账户存在，但是用户账户处于非正常状态，中止");
            return resultData;
        }

        //确定指定的金币是否满足发放的条件
        Goldcoin goldCoin = goldcoinService.get(grantGoldCoinsVo.getGoldCoidId());
        Integer gainCan = Integer.valueOf(grantGoldCoinsVo.getGoldCoinSum());//用户可获得的金币数-即申请发放的金币数
        Integer limitCan = 0;//用户被限制获取的最大值-若goldCoin不为null,则获取具体值
        Integer surplusGoinBefore = 0;//金币池剩余金币-若goldCoin不为null,则获取具体值

        if (goldCoin == null) {
            logger.info("金币活动不存在，中止");

            //记录错误信息
            goldcoinOrder.setRemarks("金币活动不存在，中止");
            //订单状态-失败
            goldcoinOrder.setOrderStatus(EBeiConstantEnum.COLDCOIN_ORDER_STATUS_FALSE.toString());//发放失败
            //订单-保存
            goldcoinOrderService.save(goldcoinOrder);

            //组织返回参数，访问失败
            ResultData<HashMap<String, Object>> resultData = new ResultData<HashMap<String, Object>>(
                    ErrorMsgEnum.ERROR.getCode(), "金币活动不存在，中止");
            return resultData;
        } else {
            //流水表增加活动id
            goldCoinTradeRecord.setGoldCoinActivity(goldCoin.getGoldcoinactivity());

            limitCan = goldCoin.getGoldcoinlimitnumber();//用户被限制获取的最大值
            surplusGoinBefore = Integer.valueOf(goldCoin.getGoldcoindistributiontotalnumber())
                    - Integer.valueOf(goldCoin.getGoldcoindistributionnumber());//金币池剩余金币

            //判断金币是否在发行阶段
            //goldcoid-status-发行/使用中 04-“审核中”： 01,“打回”： 02,“待发行”： 03,“发行/使用中”：04,“作废”： 05,“仅使用，停止发行”：06
            if (!"04".equals(goldCoin.getStatus())) {
                logger.info("金币没有在'发行'状态，中止");

                //记录错误信息
                goldcoinOrder.setRemarks("金币没有在'发行'状态，中止");
                //订单状态-失败
                goldcoinOrder.setOrderStatus(EBeiConstantEnum.COLDCOIN_ORDER_STATUS_FALSE.toString());//发放失败
                //订单-保存
                goldcoinOrderService.save(goldcoinOrder);

                //组织返回参数，访问失败
                ResultData<HashMap<String, Object>> resultData = new ResultData<HashMap<String, Object>>(
                        ErrorMsgEnum.ERROR.getCode(), "金币没有在'发行'状态，中止");
                return resultData;
            }

            //判断目前是否处于领取金币的时间
            boolean duringFlag = EBeiGoldCoinDateUtil.checkDateBetween(new Date(),
                    goldCoin.getGoldcoincollectionstart(), goldCoin.getGoldcoincollectionend());
            if (!duringFlag) {
                logger.info("不在金币活动领取期间，中止");

                //记录错误信息
                goldcoinOrder.setRemarks("不在金币活动领取期间，中止");
                //订单状态-失败
                goldcoinOrder.setOrderStatus(EBeiConstantEnum.COLDCOIN_ORDER_STATUS_FALSE.toString());//发放失败
                //订单-保存
                goldcoinOrderService.save(goldcoinOrder);

                //组织返回参数，访问失败
                ResultData<HashMap<String, Object>> resultData = new ResultData<HashMap<String, Object>>(
                        ErrorMsgEnum.ERROR.getCode(), "不在金币活动领取期间，中止");
                return resultData;
            }

            //判断金币是否发放完毕 goldCoinDistributionTotalNumber(金币发放总个数)==goldCoinDistributionNumber(金币已发放个数)
            if (goldCoin.getGoldcoindistributiontotalnumber() == goldCoin.getGoldcoindistributionnumber()) {
                logger.info("金币发放完毕，中止");

                //记录错误信息
                goldcoinOrder.setRemarks("金币发放完毕，中止");
                //订单状态-失败
                goldcoinOrder.setOrderStatus(EBeiConstantEnum.COLDCOIN_ORDER_STATUS_FALSE.toString());//发放失败
                //订单-保存
                goldcoinOrderService.save(goldcoinOrder);

                //组织返回参数，访问失败
                ResultData<HashMap<String, Object>> resultData = new ResultData<HashMap<String, Object>>(
                        ErrorMsgEnum.ERROR.getCode(), "金币发放完毕，中止");
                return resultData;
            }

        }

        //到目前为止，如果用户已注册则是处于可领取金币的状态-如果用户未注册则认为直接具有领取的资格，下面判断用户可以获得几个金币
        Integer sumGoldCoin = 0;//记录用户已经获得的金币数，初始化为0个
        if (goldCoinAccount != null) {
            //计算用户已经获得本批次的金币总数
            sumGoldCoin = goldcoinAccountDetailService.getSumGoldCoinByUserAndGoldCoinId(goldCoinAccount.getAccountid(),
                    grantGoldCoinsVo.getGoldCoidId());
        } else {
            //如果用户不存在，则先创建对象
            goldCoinAccount = new GoldcoinAccount();
            goldCoinAccount.setBranchid(grantGoldCoinsVo.getBranchId());//机构号
            goldCoinAccount.setUserid(grantGoldCoinsVo.getUserId());//易贷/转赚中用户id
            goldCoinAccount.setCreatedatetime(new Date());//账户产生时间
            goldCoinAccount.setAccountstatus(EBeiConstantEnum.GOLDCOIN_ACCOUNT_STATUS_NORMAL.toString());//金币系统账户状态：正常
            goldCoinAccount.setSubchannel(grantGoldCoinsVo.getSubChannel());
            goldCoinAccount.setUserName(grantGoldCoinsVo.getUserName());//用户名称
            goldCoinAccount.setMobile(grantGoldCoinsVo.getMobile());//用户手机号
            goldcoinAccountService.save(goldCoinAccount);
            logger.info("用户未在ebei金币系统注册，为用户注册完毕");
            sumGoldCoin = 0;//用户已经获得的金币总数为0

        }

        //1、用户获得的金币总数不大于个人可领取的最大值;2、用户本次可获得的金币数不大于可领取的金币数;  

        //判断用户获取的金币是否超过最大限制数
        //金币池一个用户可获得的自大金币数(limitCan>=fullCan)
        //排除用户已经获得的金币数后用户还可以获得的金币数 limitCan-sumGoldCoin

        //用户最大获取金币数=用户已经获取金币数,则用户不能再获得金币 
        if (limitCan == sumGoldCoin) {
            logger.info("该用户已经领取了个人金币领取数的上限，中止");

            //记录错误信息
            goldcoinOrder.setRemarks("该用户已经领取了个人金币领取数的上限，中止");
            //订单状态-失败
            goldcoinOrder.setOrderStatus(EBeiConstantEnum.COLDCOIN_ORDER_STATUS_FALSE.toString());//发放失败
            //订单-保存
            goldcoinOrderService.save(goldcoinOrder);

            //组织返回参数，访问失败
            ResultData<HashMap<String, Object>> resultData = new ResultData<HashMap<String, Object>>(
                    ErrorMsgEnum.ERROR.getCode(), "该用户已经领取了个人金币领取数的上限，中止");
            return resultData;
        }

        //如果用户（可获得最大金币数-用户已获得金币数）<用户本次申请的金币数，则终止发放金币
        //本次申请金币数
        if ((limitCan - sumGoldCoin) < gainCan) {
            logger.info("本批次用户最多可获得金币数为：" + limitCan + " ；用户已经获得的金币数为" + sumGoldCoin + " ；用户本次金币活动还允许获得的金币数为："
                    + (limitCan - sumGoldCoin) + " ；本次申请金币数为：" + grantGoldCoinsVo.getGoldCoinSum() + " ;");

            //记录错误信息
            goldcoinOrder.setRemarks("申请数大于用户本金币活动允许获得数，中止");
            //订单状态-失败
            goldcoinOrder.setOrderStatus(EBeiConstantEnum.COLDCOIN_ORDER_STATUS_FALSE.toString());//发放失败
            //订单-保存
            goldcoinOrderService.save(goldcoinOrder);

            //流水-保存
            //记录发放前和发放后-
            goldCoinTradeRecord.setAccountid(goldCoinAccount.getAccountid());

            //组织返回参数，访问失败
            ResultData<HashMap<String, Object>> resultData = new ResultData<HashMap<String, Object>>(
                    ErrorMsgEnum.ERROR.getCode(), "用户本次金币活动还允许获得的金币数为：" + (limitCan - sumGoldCoin) + " ；本次申请金币数为："
                            + grantGoldCoinsVo.getGoldCoinSum() + " ;申请数大于用户可获得数，中止");
            return resultData;
        }

        //到这里为止，用户申请的金币数就应该是用户最终获得的金币数
        //如果用户可以获得的金币数即用户申请的金币数大于该金币池剩余金币数，则停止发放金币
        if (Integer.valueOf(grantGoldCoinsVo.getGoldCoinSum()) > surplusGoinBefore) {
            logger.info(
                    "用户申请发放的金币数为：" + grantGoldCoinsVo.getGoldCoinSum() + " ；指定金币活动剩余金币数为：" + surplusGoinBefore + " ;");

            //记录错误信息
            goldcoinOrder.setRemarks("剩余金币数小于申请金币数，中止");
            //订单状态-失败
            goldcoinOrder.setOrderStatus(EBeiConstantEnum.COLDCOIN_ORDER_STATUS_FALSE.toString());//发放失败
            //订单-保存
            goldcoinOrderService.save(goldcoinOrder);

            //组织返回参数，访问失败
            ResultData<HashMap<String, Object>> resultData = new ResultData<HashMap<String, Object>>(
                    ErrorMsgEnum.ERROR.getCode(), "用户申请发放的金币数为：" + grantGoldCoinsVo.getGoldCoinSum() + " ；指定金币活动剩余金币数为："
                            + surplusGoinBefore + " ;剩余金币数小于申请金币数，中止");
            return resultData;
        }

        //开始进入用户领取金币的流程
        //给用户方法完毕之后，指定金币活动剩余金币数为
        Integer surplusGoinAfter = surplusGoinBefore - gainCan;
        //记录信息
        goldcoinOrder.setRemarks("发放成功");
        //订单状态-成功
        goldcoinOrder.setOrderStatus(EBeiConstantEnum.GOLDCOIN_ORDER_STATUS_SUCCESS.toString());//发放成功

        /**保存数据-按照发放成功处理*/
        //记录发放前和发放后-
        goldCoinTradeRecord.setAccountid(goldCoinAccount.getAccountid());
        goldCoinTradeRecord.setBeforeuse(surplusGoinBefore + "");
        goldCoinTradeRecord.setAfteruse(surplusGoinAfter + "");

        //goldcoin-修改已发放的值
        goldCoin.setGoldcoindistributionnumber(gainCan.toString());

        //获取该批次金币的生效日期和截止日期
        HashMap<String, Date> map = EBeiGoldCoinDateUtil.getBeginAndEnd(goldCoin);

        //组织账单子表的参数
        GoldcoinAccountDetail goldcoinAccountDetail = new GoldcoinAccountDetail();

        goldcoinAccountDetail.setAccountid(goldCoinAccount.getAccountid());
        goldcoinAccountDetail.setGoldcoinpoolid(grantGoldCoinsVo.getGoldCoidId());
        goldcoinAccountDetail.setBegintime(map.get("beginTime"));//
        goldcoinAccountDetail.setEndtime(map.get("endTime"));
        goldcoinAccountDetail.setSumall(gainCan + "");
        goldcoinAccountDetail.setUseall("0");
        goldcoinAccountDetail.setNouse(gainCan + "");//未使用金币数和发放金币总数一致
        goldcoinAccountDetail.setDetailstatus(EBeiConstantEnum.GOLDCOIN_ACCOUNTDETAIL_STATUS_NORMAL.toString());
        goldcoinAccountDetail.setBranchid(grantGoldCoinsVo.getBranchId());
        goldcoinAccountDetail.setUserid(grantGoldCoinsVo.getUserId());
        goldcoinAccountDetail.setEquipment(grantGoldCoinsVo.getEquipment());//设备，如安卓、IOS
        goldcoinAccountDetail.setOverduesum("0");//初始化过期金币数为0
        goldcoinAccountDetail.setRemarks(grantGoldCoinsVo.getRemarks());//描述信息，比如 注册获得10个金币
        //发送成功
        try {
            goldcoinAccountDetailService.saveNewGoldCoinGrant(goldcoinOrder, goldCoinTradeRecord, goldcoinAccountDetail,
                    goldCoin);
            logger.info("给用户发放金币前，指定金币活动可发放金币总数为:" + surplusGoinBefore + " ;给用户发放之后，指定金币可发放金币总数为：" + surplusGoinAfter
                    + " ;用户获得金币数为：" + gainCan);

            //组织返回参数，访问成功
            ResultData<HashMap<String, Object>> resultData = new ResultData<HashMap<String, Object>>(
                    ErrorMsgEnum.SUCCESS.getCode(), "给用户发放金币前，指定金币活动可发放金币总数为:" + surplusGoinBefore
                            + " ;给用户发放之后，指定金币可发放金币总数为：" + surplusGoinAfter + " ;用户获得金币数为：" + gainCan);
            return resultData;

        } catch (Exception e) {
            /**订单失败*/
            //记录错误信息
            goldcoinOrder.setRemarks("系统异常，中止");
            //订单状态-失败
            goldcoinOrder.setOrderStatus(EBeiConstantEnum.COLDCOIN_ORDER_STATUS_FALSE.toString());//发放失败
            //订单-保存
            goldcoinOrderService.save(goldcoinOrder);

            //数据库访问异常，数据保存失败
            logger.info("数据库访问异常，数据保存失败");

            //组织返回参数，访问成功
            ResultData<HashMap<String, Object>> resultData = new ResultData<HashMap<String, Object>>(
                    ErrorMsgEnum.ERROR.getCode(), "数据库访问异常，数据保存失败");

            return resultData;
        }

    }

    @Override
    public ResultData<HashMap<String, Object>> UseGoldCoin(UseGoldCoinVo useGoldCoinVo) {
        logger.info("访问开始，入参：" + useGoldCoinVo.toString());
        //机构id 存储在Office实体中
        //订单表增加一条记录
        GoldcoinOrder goldcoinOrder = new GoldcoinOrder();
        goldcoinOrder.setOrderid(useGoldCoinVo.getOrderId());
        goldcoinOrder.setBranchid(useGoldCoinVo.getBranchId());
        goldcoinOrder.setSubchannel(useGoldCoinVo.getSubChannel());
        goldcoinOrder.setOrderStatus(EBeiConstantEnum.GOLDCOIN_ORDER_STATUS_INITIALIZE.toString());
        goldcoinOrder.setTradetime(useGoldCoinVo.getTradeTime());//交易时间
        goldcoinOrder.setTradetype(EBeiConstantEnum.GOLDCOIN_ORDER_TYPE_USER.toString());//使用金币
        goldcoinOrder.setGoldcoinsum(useGoldCoinVo.getGoldCoinSum());

        //流水表增加一条记录
        GoldcoinTradeRecord goldCoinTradeRecord = new GoldcoinTradeRecord();
        goldCoinTradeRecord.setAccountid(EBeiConstantEnum.GOLDCOIN_DEFAULT_VALUE.toString());//关联 goldcoin_account
        goldCoinTradeRecord.setDetailid(EBeiConstantEnum.GOLDCOIN_DEFAULT_VALUE.toString());//关联 goldcoin_account_detail
        goldCoinTradeRecord.setRecordType(EBeiConstantEnum.GOLDCOIN_TRADERECORD_STATUS_USE.toString());//记录状态,00-获取，01-使用，02-过期，03冲正，04-作废
        goldCoinTradeRecord.setGoldnumber(useGoldCoinVo.getGoldCoinSum());//交易金币数量
        goldCoinTradeRecord.setBranchid(useGoldCoinVo.getBranchId());
        goldCoinTradeRecord.setBusinessId(useGoldCoinVo.getOrderId());//交易流水号
        goldCoinTradeRecord.setEquipment(useGoldCoinVo.getEquipment());//设备，如IOS

        //查询该用户是否存在
        GoldcoinAccount goldCoinAccount = goldcoinAccountService.getGoldcoinAccountCheck(useGoldCoinVo.getUserId(),
                useGoldCoinVo.getBranchId());

        //如果用户为null,或者用户不为null但是用户账户不是正常状态，则中止
        if (goldCoinAccount == null || (!EBeiConstantEnum.GOLDCOIN_ACCOUNT_STATUS_NORMAL.toString()
                .equals(goldCoinAccount.getAccountstatus()))) {
            logger.info("金币账户处于非正常状态，中止");

            //记录错误信息
            goldcoinOrder.setRemarks("金币账户处于非正常状态，中止");
            //订单状态-失败
            goldcoinOrder.setOrderStatus(EBeiConstantEnum.COLDCOIN_ORDER_STATUS_FALSE.toString());//使用失败
            //订单-保存
            goldcoinOrderService.save(goldcoinOrder);

            //组织返回参数，访问失败
            ResultData<HashMap<String, Object>> resultData = new ResultData<HashMap<String, Object>>(
                    ErrorMsgEnum.ERROR.getCode(), "金币账户处于非正常状态，中止");
            return resultData;
        }

        Integer useCoin = Integer.valueOf(useGoldCoinVo.getGoldCoinSum());//申请使用的金币数量
        Integer sumAvailableCoin = goldcoinAccountDetailService
                .getSumAvailableGoldCoinByAccountId(goldCoinAccount.getAccountid());//实际可以使用的金币数量
        //如果申请使用的金币数大于可供使用的金币数，则中止
        if (useCoin > sumAvailableCoin) {
            logger.info("申请使用金币数为：" + useCoin + " ；实际可用的金币数为：" + sumAvailableCoin + " ；");

            //记录错误信息
            goldcoinOrder.setRemarks("实际可用的金币数不足，中止");
            //订单状态-失败
            goldcoinOrder.setOrderStatus(EBeiConstantEnum.COLDCOIN_ORDER_STATUS_FALSE.toString());//使用失败
            //订单-保存
            goldcoinOrderService.save(goldcoinOrder);

            //组织返回参数，访问失败
            ResultData<HashMap<String, Object>> resultData = new ResultData<HashMap<String, Object>>(
                    ErrorMsgEnum.ERROR.getCode(),
                    "申请使用金币数为：" + useCoin + " ；实际可用的金币数为：" + sumAvailableCoin + " ；实际可用的金币数不足，中止");
            return resultData;
        }
        Integer useAfterAvaliableCoin = sumAvailableCoin - useCoin;//使用完之后，剩余可用的金币总数

        //如果到这里，则表示可以正常方法金币，开始发放金币的流程

        //将可供使用的金币数，按照“过期时间”升序排序，即20170101、20170102、20170103，包含 账户字表id，可用金币个数，失效时间
        ArrayList<GoldcoinAccountDetail> list = goldcoinAccountDetailService
                .getParamsAvailableGoldCoinByAccountId(goldCoinAccount.getAccountid());//实际可以使用的金币数量以及列表

        //执行用户金币数减少的数据库操作
        goldCoinTradeRecord.setAccountid(goldCoinAccount.getAccountid());

        //按成功预处理数据
        goldcoinOrder.setRemarks("成功");
        try {
            goldcoinAccountDetailService.updateGoldCoinDetailsByUseCoin(useCoin, list, sumAvailableCoin, goldcoinOrder,
                    goldCoinTradeRecord);
            //金币扣除成功
            logger.info("申请使用金币数为：" + useCoin + " ；实际可用的金币数为：" + sumAvailableCoin + " ；操作成功");

            //组织返回参数，访问成功
            ResultData<HashMap<String, Object>> resultData = new ResultData<HashMap<String, Object>>(
                    ErrorMsgEnum.SUCCESS.getCode(),
                    "申请使用金币数为：" + useCoin + " ；实际可用的金币数为：" + sumAvailableCoin + " ；操作成功");
            return resultData;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.info("申请使用金币数为：" + useCoin + " ；实际可用的金币数为：" + sumAvailableCoin + " ；");

            //记录错误信息
            goldcoinOrder.setRemarks("数据库操作异常，中止");
            //订单状态-失败
            goldcoinOrder.setOrderStatus(EBeiConstantEnum.COLDCOIN_ORDER_STATUS_FALSE.toString());//使用失败
            //订单-保存
            goldcoinOrderService.save(goldcoinOrder);

            //组织返回参数，访问失败
            ResultData<HashMap<String, Object>> resultData = new ResultData<HashMap<String, Object>>(
                    ErrorMsgEnum.ERROR.getCode(),
                    "申请使用金币数为：" + useCoin + " ；实际可用的金币数为：" + sumAvailableCoin + " ；数据库操作异常，中止");
            return resultData;
        }

    }

    @Override
    public ArrayList<GoldCoinSettlementResp> getGoldCoinActivityCoinSumByDate(
            GoldCoinSettlementReq goldCoinSettlementReq) {
        ArrayList<GoldCoinSettlementResp> list = new ArrayList<GoldCoinSettlementResp>();

        ArrayList<Map<String, Object>> mapList = goldcoinTradeRecordService
                .getGoldCoinActivityCoinSumByDate(goldCoinSettlementReq);
        for (Map<String, Object> m : mapList) {
            GoldCoinSettlementResp goldCoinSettlementResp = new GoldCoinSettlementResp();
            goldCoinSettlementResp.setActivityId(m.get("activityId").toString());//eBei 金币活动id
            goldCoinSettlementResp.setBranchId(goldCoinSettlementReq.getBranchId());//易贷  MS_YIDAI   转赚   MS_ZZ
            goldCoinSettlementResp.setCountSum(m.get("countSum").toString());//发放金币总数
            goldCoinSettlementResp.setDate(goldCoinSettlementReq.getDate());//统计日期
            list.add(goldCoinSettlementResp);
        }
        return list;
    }

}
